package com.kajespero.domain.template.impl;

import java.util.HashMap;
import java.util.Map;

import com.kajespero.dao.template.impl.TemplateDAOImpl;
import com.kajespero.dao.velocity.VelocityProvider;
import com.kajespero.dao.velocity.impl.VelocityProviderImpl;
import com.kajespero.domain.commons.impl.AbstractReadWriteDomain;
import com.kajespero.domain.template.TemplateDomain;
import com.kajespero.enums.TemplateType;
import com.kajespero.model.TemplateBean;
import com.kajespero.vo.TemplateVO;

public class TemplateDomainImpl extends AbstractReadWriteDomain<TemplateDAOImpl, TemplateBean, Long> implements TemplateDomain {

	private static final VelocityProvider VELOCITY_PROVIDER = new VelocityProviderImpl();
	
	@Override
	public String parseTemplate(TemplateVO template, Map<String, Object> map) {
		if(template.getType().equals(TemplateType.VELOCITY)){
			return transformTemplateToHtml(template.getValue(), map);
		}
		return template.getValue();
	}
	
	private String transformTemplateToHtml(String templateValue, Map<String, Object> map){
		return VELOCITY_PROVIDER.templateToHtml(templateValue, map);
	}

	@Override
	public TemplateBean findByName(String name) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("title", name);
		return getDAO().findBy(map);
	}

}
